package com.zhs.gccs.activity.gccs;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.annimon.stream.Stream;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.shape.view.ShapeButton;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.zhs.gccs.R;
import com.zhs.gccs.adapter.AdminGccsPeopleListAdapter;
import com.zhs.gccs.base.BaseActivity;
import com.zhs.gccs.http.Api;
import com.zhs.gccs.http.ProgressObserver;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.ToastUtils;
import com.zhs.gccs.vo.AdminPeopleBean;
import com.zhs.gccs.vo.AdminPeopleListBean;
import com.zhs.gccs.vo.AdminTaskBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chengzf on 2024/7/19.
 */
public class AdminAssignPersonnelActivity extends BaseActivity {
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.fame_bar)
    FrameLayout fame_bar;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.btn_back)
    ShapeButton btn_back;
    @BindView(R.id.btn_next)
    ShapeButton btn_next;

    private List<AdminPeopleBean> gccsTasks;
    private AdminGccsPeopleListAdapter gccsTaskAdapter;
    private int pageNum = 0;
    private int pageSize = 10;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_assign_personnel_layout);
        ImmersionBar.with(this)
                .transparentBar()
                .statusBarDarkFont(true)
                .init();
        ButterKnife.bind(this);
        fame_bar.setPadding(0, ImmersionBar.getStatusBarHeight(this), 0, 0);
        gccsTasks = new ArrayList<>();
        gccsTaskAdapter  = new AdminGccsPeopleListAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(gccsTaskAdapter);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(false);

        gccsTaskAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                for (int i1 = 0; i1 < gccsTaskAdapter.getData().size(); i1++) {
                    AdminPeopleBean taskBean = gccsTaskAdapter.getData().get(i1);
                    if (i == i1){
                        taskBean.setIcCheck(!taskBean.isIcCheck());
                    }else {
                        taskBean.setIcCheck(false);
                    }
                }
                gccsTaskAdapter.setNewData(gccsTaskAdapter.getData());
            }
        });


        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNum = 0;
                getGccsUserList();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                getGccsUserList();
            }
        });
        refreshLayout.setEnableLoadMore(false);

        initView();
        getGccsUserList();
    }

    private void initView() {
        iv_back.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });

        btn_back.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });

        btn_next.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                AdminPeopleBean peopleBean = null;
                for (int i = 0; i < gccsTaskAdapter.getData().size(); i++) {
                    AdminPeopleBean bean = gccsTaskAdapter.getData().get(i);
                    if (bean.isIcCheck()){
                        peopleBean = bean;
                        break;
                    }
                }
                if (peopleBean != null){
                    AdminTaskBean taskBean = (AdminTaskBean) getIntent().getSerializableExtra("task");
                    AdminSendTaskConfirmActivity.start(AdminAssignPersonnelActivity.this, taskBean, peopleBean);
                }else {
                    ToastUtils.toast("请先选择采集员");
                }
            }
        });
    }

    private void getGccsUserList(){
        showDialog();
        toSubscribeSingleOb(Api.getDefault().getCjUsers(),
                new ProgressObserver(this, false) {
                    @Override
                    protected void _onNext(String result) {
                        AdminPeopleListBean bean = new Gson().
                                fromJson(result, AdminPeopleListBean.class);
                        //GsonImpl.get().toObject(result, SmTaskBean.class);
                        if(pageNum==0){
                            gccsTasks.clear();
                        }
                        if (bean != null && bean.getCode() == 200 &&
                                bean.getData() != null&& bean.getData().size() >0 ) {
                            List<AdminPeopleBean> list = Stream.of(bean.getData()).
                                    filter(i -> "0".equals(i.getStatus())).toList();
                            gccsTasks.addAll(list);
                            finishLoadMore();
                        }else {
                            finishLoadMore();
                        }
                        gccsTaskAdapter.setNewData(gccsTasks);
                    }
                    @Override
                    protected void _onError(String result) {
                        finishLoadMore();
                    }
                });
    }

    private void finishLoadMore() {
        dismissDialog();
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    public static void start(Context context, AdminTaskBean taskBean) {
        Intent intent = new Intent(context, AdminAssignPersonnelActivity.class);
        intent.putExtra("task", taskBean);
        context.startActivity(intent);
    }
}
